package com.atguigu.eduorder.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.service.OrderService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author ZhuangLiJiao
 * @since 2021-10-03
 */
@Api(description = "订单管理")
@RestController
@CrossOrigin
@RequestMapping("/eduorder/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //创建订单
    @ApiOperation("生成订单")
    @PostMapping("createOrder/{courseId}")
    public R saveOrder(@PathVariable String courseId, HttpServletRequest request) {
        //创建订单，返回订单号
        //使用Jwt工具获取用户的Id
        String orderNo = orderService.createOrders(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderNo);
    }

    //根据订单id查询订单信息
    @ApiOperation("根据订单id查询订单信息")
    @GetMapping("getOrderInfo/{orderId}")
    public R getOrderInfo(@PathVariable String orderId) {
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getId, orderId);
        Order order = orderService.getOne(wrapper);
        return R.ok().data("item", order);
    }



    //根据课程id、用户id、查询订单的支付状态
    @ApiOperation("根据课程id、用户id、查询订单的支付状态")
    @GetMapping("isBuyCourse/{courseid}/{memberId}")
    public boolean isBuyCourse(@PathVariable("courseid") String courseId, @PathVariable("memberId") String memberId) {
        System.out.println("进入 service-order 模块 的isBugCourse方法");
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Order::getCourseId, courseId);
        wrapper.eq(Order::getMemberId, memberId);
        wrapper.eq(Order::getStatus, "1");//1表示已经支付
        return orderService.count(wrapper) > 0;
    }



}

